package poi;



import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;




public class GeoServlet extends GenericServlet {
	
	private static final long serialVersionUID = -191123335401967525L;
	
	private static Logger log = Logger.getLogger("POISystem.GeoServlet");
	
	private boolean wasError;
	POIDatabase body;
	LogFormatter lf = null;
		
	public GeoServlet()	{
		wasError = false;
		LogFormatter.setLogFormat();
		body = new POIDatabase();
		log.info("GeoServlet object created.");
	}
	
	public String createResponse(String filePath) {
		if (!body.connectSource(filePath)) {
			wasError=true;
		}
		String result = new String();
		result = "";
        if (wasError) {
			result = "Error reading from CSV file!";
		} else {
			LinkedList<GeoInfo> li = body.buildList(null);
       	    Iterator<GeoInfo> it = li.iterator();
       		while (it.hasNext()) {
       			GeoInfo val = it.next();
       			result += val.toString() + "<br>";
        	}
        }
    	return result;
	}
        	

	public void service(ServletRequest request, ServletResponse response) 
	throws ServletException {
		response.setContentType("text/html");
        PrintWriter out;
		try {
			out = response.getWriter();
			out.println(createResponse("POISystem\\poidata.csv"));
	        out.close();
		} catch (IOException e) {
			log.severe("Can't send servlet responce.");
			e.printStackTrace();
		}
	}
	protected void finalize()
	{
		log.info("GeoServlet object deleted.");
	}
}
